package com.juc.locksupport;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author Zan
 * @Create 2024/5/8 21:19
 * @ClassName : LockSupportDemo
 * @Description : t1线程等待1秒钟，1秒钟后t2线程唤醒t1线程继续工作
 */
public class LockSupportDemo {
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "\t --- come in" + System.currentTimeMillis());
            LockSupport.park();
            System.out.println(Thread.currentThread().getName() + "\t --- 被唤醒" + System.currentTimeMillis());
        }, "t1");
        t1.start();

//        try {
//            TimeUnit.SECONDS.sleep(1);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        new Thread(() -> {
            LockSupport.unpark(t1);
            System.out.println(Thread.currentThread().getName() + "\t --- 唤醒通知");
        }, "t2").start();
    }

    private static void lockCondition() {
        Lock lock = new ReentrantLock();

        // Condition类可以实现等待/通知模式
        Condition condition = lock.newCondition();

        new Thread(() -> {
//            try {
//                TimeUnit.SECONDS.sleep(1);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName() + "\t --- come in");
                condition.await();
                System.out.println(Thread.currentThread().getName() + "\t --- 被唤醒");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }, "t1").start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            lock.lock();
            try {
                condition.signal();
                System.out.println(Thread.currentThread().getName() + "\t --- 唤醒通知");
            } finally {
                lock.unlock();
            }
        }, "t2").start();
    }

    private static void waitAndnotify() {
        Object objLock = new Object();

        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (objLock) {
                System.out.println(Thread.currentThread().getName() + "\t --- come in");
                try {
                    objLock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "\t --- 被唤醒");
            }
        }, "t1").start();

//        try {
//            TimeUnit.SECONDS.sleep(1);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        new Thread(() -> {
            synchronized (objLock) {
                objLock.notify();
                System.out.println(Thread.currentThread().getName() + "\t --- 发出通知");
            }
        }, "t2").start();
    }
}
